/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sunrise.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

/**
 *
 * @author frank
 */
@Entity
@NamedQueries({
        @NamedQuery(name = Calendar.findByUser, query = "SELECT c FROM Calendar c WHERE c.owner = :owner")
})
public class Calendar implements Serializable {          
    private static final long serialVersionUID = 1L;
    
    public static final String findByUser = "Calendar.findByUser";

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    @Column(length=45, nullable=false)
    private String description;
     
    @Column(nullable=false)
    private boolean publicCalendar;
    
    @ManyToOne
    private User owner;
    
    @OneToMany(mappedBy = "calendar")
    private List<Event> events;
    
    public Calendar() {
        this("main", null);
    }
    
    public Calendar(User owner) {
        this("main", owner);
    }
    
    public Calendar(String description) {
        this(description, null);
    }
    
    public Calendar(String description, User owner) {
        this.description = description;
        this.publicCalendar = false;
        this.owner = owner;
    }
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Calendar)) {
            return false;
        }
        Calendar other = (Calendar) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public boolean isPublicCalendar() {
        return publicCalendar;
    }

    public void setPublicCalendar(boolean publicCalendar) {
        this.publicCalendar = publicCalendar;
    }

    public User getOwner() {
        return owner;
    }

    public void setOwner(User owner) {
        this.owner = owner;
    }

    public List<Event> getEvents() {
        if (events==null) {
            events = new ArrayList<Event>();
        }
        return events;
    }

    public void setEvents(List<Event> events) {
        if (events==null) {
            events = new ArrayList<Event>();
        }
        this.events = events;
    }
    
    public void addEvent(Event event) {
        if(events == null)
            events = new ArrayList<Event>();
        this.events.add(event);
    }

    @Override
    public String toString() {
        return "com.sunrise.entity.Calendar[ id=" + id + " ]";
    }
    
}
